<?php
require_once("../bin/MysqlConn.php");

function initDB() {
//   $query = "source ../db/backup-file.sql;";
   $querys = array();
   $conn = new MysqlConn();

   // customers
   array_push($querys, "");
   array_push($querys, "DROP TABLE IF EXISTS `customers`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `customers` (" .
      "  `CUS_ID` varchar(20) NOT NULL," .
      "  `CUS_PWD` varchar(20) character set utf8 collate utf8_bin NOT NULL," .
      "  `CUS_REALNAME` varchar(20) NOT NULL," .
      "  `CUS_PHONE` varchar(20) NOT NULL," .
      "  `CUS_ADDRESS` varchar(50) NOT NULL," .
      "  `CUS_REWARDPOINTS` int(20) default NULL," .
      "  `CUS_REMARKS` varchar(50) default NULL," .
      "  PRIMARY KEY  (`CUS_ID`)" .
      ") ENGINE=MyISAM DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");

   // managers
   array_push($querys, "DROP TABLE IF EXISTS `managers`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `managers` (" .
      "   `MAN_ID` varchar(20) NOT NULL," .
      "   `MAN_PWD` varchar(20) character set utf8 collate utf8_bin NOT NULL," .
      "   `MAN_LEVEL` int(2) NOT NULL," .
      "   PRIMARY KEY  (`MAN_ID`)" .
      ") ENGINE=MyISAM DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");
   array_push($querys, "LOCK TABLES `managers` WRITE;");
   array_push($querys, "INSERT INTO `managers` VALUES ('admin','success123',1);");
   array_push($querys, "UNLOCK TABLES;");

   // menuitems;
   array_push($querys, "DROP TABLE IF EXISTS `menuitems`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `menuitems` (" .
      "   `MEN_ID` int(11) NOT NULL auto_increment," .
      "   `MEN_ORDERID` int(11) NOT NULL," .
      "   `MEN_SNACKID` int(11) NOT NULL," .
      "   `MEN_COUNT` int(11) NOT NULL," .
      "   PRIMARY KEY  (`MEN_ID`)" .
      ") ENGINE=MyISAM AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");

   // orders

   array_push($querys, "DROP TABLE IF EXISTS `orders`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `orders` (" .
      "  `ORD_ID` int(11) NOT NULL auto_increment," .
      "  `ORD_STARTTIME` datetime NOT NULL," .
      "  `ORD_WILLTIME` datetime NOT NULL," .
      "  `ORD_STATUS` int(2) default NULL," .
      "  `ORD_CUSTOMER` varchar(20) NOT NULL," .
      "  `ORD_ADDRESS` varchar(100) NOT NULL," .
      "  `ORD_PHONE` varchar(20) default NULL," .
      "  `ORD_REQUEST` varchar(50) default NULL," .
      "  `ORD_REWARD` varchar(20) default NULL," .
      "  `ORD_PRICE` int(11) default NULL," .
      "  PRIMARY KEY  (`ORD_ID`)" .
      ") ENGINE=MyISAM AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");

   // ostatus;
   array_push($querys, "DROP TABLE IF EXISTS `ostatus`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `ostatus` (" .
      "  `OST_ID` int(11) NOT NULL," .
      "  `OST_DESCRIPTION` varchar(20) default NULL," .
      "  PRIMARY KEY  (`OST_ID`)" .
      ") ENGINE=MyISAM DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");
   array_push($querys, "LOCK TABLES `ostatus` WRITE;");
   array_push($querys, "INSERT INTO `ostatus` VALUES (0,'created'),(1,'verified'),(2,'sended'),(3,'completed');");
   array_push($querys, "UNLOCK TABLES;");

   // snacks;
   array_push($querys, "DROP TABLE IF EXISTS `snacks`;");
   array_push($querys, "SET @saved_cs_client     = @@character_set_client;");
   array_push($querys, "SET character_set_client = utf8;");
   array_push($querys, "CREATE TABLE `snacks` (" .
      "  `SNA_ID` int(9) NOT NULL auto_increment," .
      "  `SNA_NAME` varchar(20) NOT NULL," .
      "  `SNA_PRICE` int(9) NOT NULL," .
      "  `SNA_REMARKS` varchar(50) default NULL," .
      "  `SNA_PICURL` varchar(50) default NULL," .
      "  `SNA_TYPE` int(4) NOT NULL," .
      "  PRIMARY KEY  (`SNA_ID`)" .
      ") ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;");
   array_push($querys, "SET character_set_client = @saved_cs_client;");

   for($i = 0; $i < count($querys); $i++) {
      $query = $querys[$i];
      $conn->query($query);

      if($conn->getErrNo() > 0) {
         echo $conn->getError();
      }
      else {
         echo "$i : Success<br/>";
      }
   }
}

function init() {
   initDB();
}

init();
?>